#!/usr/bin/perl

use DBI;

my $db = "va_stratlab";
my $ip = "localhost";
my $user = "va";
my $password = "dummy";

if ( $#ARGV < 0 ) {
    print "Usage: getprice.pl [stock symbol code] ([date from] ([date to]))\n";
    exit 0;
}

my $dbh=DBI->connect("dbi:mysql:$db:$ip", $user, $password) or die "Can't connect to DB";

$sql = "SELECT stockprice.* FROM stockname LEFT JOIN stockprice ON (id=stock_id) WHERE code='".$ARGV[0]."'";
if ( $#ARGV > 0 ) {
    $sql .= " AND date>='".$ARGV[1]."'";
}
if ( $#ARGV > 1 ) {
    $sql .= " AND date<='".$ARGV[2]."'";
}

my $sth = $dbh->prepare($sql." ORDER BY `date`");
my $ret = $sth->execute;
my $i = 0;
while ( @ret = $sth->fetchrow_array() ) {
    if ( $i == 0 ) {
	printf "\n%8s%13s%13s%13s%13s%9s%9s", "date", "open", "high", "low", "close", "vol", "oi";
	print "\n\n";
	$i = 0;
    }

    printf "%8s%13s%13s%13s%13s%9s%9s\n", $ret[1], $ret[2], $ret[3], $ret[4], $ret[5], $ret[6], $ret[7];

    $i++;
}

1;
